
String.prototype.padStart = function (length, str) {
  if (!str) {
    str = "0";
  }
  var x = length - this.length;
  var temp = "";
  for (var i = 0; i < x; i++) {
    temp += str;
  }
  return temp + String(this);
};

/**
 * app通知状态变化
 * @return
 */
function stateChange(type, value) {
  console.log(`stateChange:type:${type},value:${value}`);
  if (!vue) {
    return;
  }

  if (type === "network") {
    /* 0:无网络连接
     * 1：有线网络
     * 2：无线网络
     * 3: 未知网络
     * */
    if (value == 0) {
      vue.networkIcon = "./img/net_err.png";
    } else if (value == 1) {
      vue.networkIcon = "./img/net_wired.png";
    } else if (value == 2) {
      vue.networkIcon = "./img/net.png";
    } else {
      vue.networkIcon = "./img/net.png";
    }
  } else if (type === "onStart") {
    //android 页面进入
    vue.getTime();
  } else if (type === "onStop") {
    //android 页面离开
    clearTimeout(vue.timeTaskCode);
  } else if (type === "package") {
    //有app安装或卸载
    vue.loadApp();
  }
}

var vue = new Vue({
  el: ".page",
  data: function () {
    return {
      time: "",
      apps: [],
      networkIcon: "./img/net_err.png",
      weekDay: [
        "星期天",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ],
      butEls: [],
      timeTaskCode: 0,
      clickCount:{}
    };
  },
  methods: {
    getTime: function () {
      var date = new Date();
      var year = date.getFullYear();
      var month = (date.getMonth() + 1).toString().padStart(2, "0");
      var day = date.getDate().toString().padStart(2, "0");
      var weekDay = this.weekDay[date.getDay()];
      var hours = date.getHours().toString().padStart(2, "0");
      var minutes = date.getMinutes().toString().padStart(2, "0");
      this.time = `${year}-${month}-${day}  ${weekDay}  ${hours}:${minutes}`;

      this.timeTaskCode = setTimeout(
        this.getTime,
        (60 - date.getSeconds()) * 1000
      );
    },
    clickListener: function (app) {
      if (andClient) {
        andClient.startActivity(app.pkm, app.akm);
        var count = this.clickCount[app.pkm];
        if (!count) {
          count = 0;
        }
        this.clickCount[app.pkm] = ++count;
        this.saveClickCount();
      }
    },
    loadApp: function () {
      var slef = this;
      if (!andClient) {
        return;
      }
      var appArr = andClient.loadApps();
      if (!appArr) {
        return;
      }
      appArr = JSON.parse(appArr);
      if (!appArr) {
        return;
      }

      var selfName = andClient.getSelfPkName();
      var user = [];
      var sys = [];

      for (var i = 0; i < appArr.length; i++) {
        var appInfo = appArr[i];
        if (appInfo.pkm === selfName) {
          continue;
        }
        if (appInfo.isSys) {
          sys.push(appInfo);
        } else {
          user.push(appInfo);
        }
      }
      this.appSort(user);
      this.appSort(sys);

      this.apps.length = 0;
      if (user && user.length > 0) {
        this.apps.push({ type: "sys", arr: user });
      }
      if (sys && sys.length > 0) {
        this.apps.push({ type: "user", arr: sys });
      }
    
      slef.butEls.length = 0;
      this.$nextTick(function () {
        var $obj = $("button");
        for (var i = 0; i < $obj.length; i++) {
          slef.butEls.push($obj[i]);
        }
        slef.butEls[2].focus();
      });
    },
    appSort: function (arr) {
      var self = this;
      arr.sort(function (app1, app2) {
        var count1 = self.clickCount[app1.pkm];
        var count2 =  self.clickCount[app2.pkm];
        if (!count1) {
          count1 = 0;
        }
        if (!count2) {
          count2 = 0;
        }
        return count2 - count1;
      });
    },
    getNetMode: function () {
      if (!andClient) {
        return;
      }
      andClient.getNetMode();
    },
    gotoSetting: function () {
      if (!andClient) {
        return;
      }

      andClient.gotoSetting();
    },
    gotoWifiSeting: function () {
      if (!andClient) {
        return;
      }

      andClient.gotoWifiSeting();
    },
    getClickCount:function(){
      var json = localStorage.getItem("clickCount");
      var temp = {};
      if(json){
        temp = JSON.parse(json);
      }
      this.clickCount = temp;
    },
    saveClickCount:function(){
      var json = JSON.stringify(this.clickCount);
      localStorage.setItem("clickCount",json);
    },
    onfocus: function (event) {
      var temp =
        event.target.offsetTop -
        window.innerHeight / 2 +
        event.target.clientHeight / 2;
      var time = Math.abs(temp*1.5);
      if(time > 300){
        time = 300;
      }
      $("html, body").animate({ scrollTop: temp }, 300);
    },
    onkeyDown: function (event) {
      if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
        if (!this.butEls) {
          return;
        }
        var activeElementIndex = this.butEls.indexOf(document.activeElement);
        if (event.key === "ArrowRight") {
          activeElementIndex++;
        } else {
          activeElementIndex--;
        }
        if (
          activeElementIndex >= this.butEls.length ||
          activeElementIndex < 0
        ) {
          return;
        }
        this.butEls[activeElementIndex].focus();
        return false;
      }
    },
  },
  mounted: function () {
    this.getTime();
    this.getNetMode();
    this.getClickCount();
    this.loadApp();
    document.addEventListener("keydown",this.onkeyDown)
  },
});


